package com.wzs.workbench.web.controller;

import com.wzs.utils.UUIDUtil;
import com.wzs.vo.PaginationVO;
import com.wzs.workbench.domain.Chapter;
import com.wzs.workbench.domain.Comment;
import com.wzs.workbench.domain.Novel;
import com.wzs.workbench.service.CommentService;
import com.wzs.workbench.service.NovelService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/novel/")
public class NovelController {

    @Resource
    private NovelService novelService;

    @Resource
    private CommentService commentService;

    @ResponseBody
    @RequestMapping("pageList.do")
    public PaginationVO<Chapter> PageList(@RequestParam("pageNo") String pageNoStr,
                                          @RequestParam("pageSize") String pageSizeStr,
                                          String id,String novelId) {
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //计算出略过的记录数
        int skipCount = (pageNo - 1) * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("skipCount", skipCount);
        map.put("id", id);
        map.put("novelId", novelId);
        return novelService.pageList(map);
    }

    @RequestMapping("novelPageList.do")
    @ResponseBody
    public PaginationVO<Novel> novelPageList(@RequestParam("pageNo") String pageNoStr,
                                             @RequestParam("pageSize") String pageSizeStr,
                                             String novelName, String novelColumn) {
        System.out.println("根据小说所属栏目来查询小说列表");
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //计算出略过的记录数
        int skipCount = (pageNo - 1) * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("skipCount", skipCount);
        map.put("novelName", novelName);
        map.put("novelColumn", novelColumn);
        return novelService.novelPageList(map);
    }

    @RequestMapping("pageListByAid.do")
    @ResponseBody
    public PaginationVO<Novel> pageListByAid(@RequestParam("pageNo") String pageNoStr,
                                             @RequestParam("pageSize") String pageSizeStr,
                                             String authorId,String novelName) {
        System.out.println("根据作者id查询小说列表");
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //计算出略过的记录数
        int skipCount = (pageNo - 1) * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("skipCount", skipCount);
        map.put("authorId", authorId);
        map.put("novelName", novelName);
        return novelService.pageListByAid(map);
    }

    @RequestMapping("getCommentListByAid.do")
    @ResponseBody
    public List<Comment> getCommentListByAid(String novelId) {

        System.out.println("根据小说的id,取得评论信息列表");

        return commentService.getCommentListByAid(novelId);
    }

    @RequestMapping("saveComment.do")
    @ResponseBody
    public Map<String, Object> saveComment(Comment comment) {

        System.out.println("添加备注操作");
        String id = UUIDUtil.getUUID();
        comment.setId(id);

        boolean flag = commentService.saveComment(comment);

        Map<String, Object> map = new HashMap<>();
        map.put("success", flag);
        map.put("c", comment);

        return map;
    }

    @RequestMapping("deleteComment.do")
    @ResponseBody
    public boolean deleteComment(String id) {

        System.out.println("删除备注操作");

        return commentService.deleteRemark(id);
    }

    @RequestMapping("editComment.do")
    @ResponseBody
    public boolean editComment(String id) {

        System.out.println("删除备注操作");

        return commentService.deleteRemark(id);
    }

    @RequestMapping("updateComment.do")
    @ResponseBody
    public Map<String, Object> updateComment(Comment comment) {

        System.out.println("执行备注更新操作");

        boolean flag = commentService.updateComment(comment);

        Map<String, Object> map = new HashMap<>();
        map.put("success", flag);
        map.put("c", comment);

        return map;
    }

    @RequestMapping("fantasy/PageListByNc.do")
    public String PageListByNcF(Model model, String novelColumn) {

        System.out.println("根据小说所属栏目来查询小说列表");

        model.addAttribute("nc", novelColumn);

        return "workbench/fantasy/index";
    }

    @RequestMapping("action/PageListByNc.do")
    public String PageListByNcA(Model model, String novelColumn) {

        System.out.println("根据小说所属栏目来查询小说列表");

        model.addAttribute("nc", novelColumn);

        return "workbench/action/index";
    }

    @RequestMapping("history/PageListByNc.do")
    public String PageListByNcH(Model model, String novelColumn) {

        System.out.println("根据小说所属栏目来查询小说列表");

        model.addAttribute("nc", novelColumn);

        return "workbench/history/index";
    }

    @RequestMapping("suspense/PageListByNc.do")
    public String PageListByNcS(Model model, String novelColumn) {

        System.out.println("根据小说所属栏目来查询小说列表");

        model.addAttribute("nc", novelColumn);

        return "workbench/suspense/index";
    }

    @RequestMapping("city/PageListByNc.do")
    public String PageListByNcC(Model model, String novelColumn) {

        System.out.println("根据小说所属栏目来查询小说列表");

        model.addAttribute("nc", novelColumn);

        return "workbench/city/index";
    }

    @RequestMapping("fantasy.do")
    public String fantasy(Model model, String novelId) {

        System.out.println("根据小说所属栏目来查询小说列表");

        Novel novel = novelService.getById(novelId);

        model.addAttribute("n", novel);

        return "workbench/fantasy/single";
    }

    @RequestMapping("putNovel.do")
    @ResponseBody
    public boolean putNovel(Novel novel) {

        System.out.println("创建小说操作");
        return novelService.putNovel(novel);

    }

    @RequestMapping("deleteNovel.do")
    @ResponseBody
    public boolean deleteNovel(HttpServletRequest request) {
        System.out.println("执行到删除小说的操作");

        String[] ids = request.getParameterValues("id");

        return novelService.deleteNovel(ids);
    }

    @RequestMapping("editNovel.do")
    public String editNovel(Model model, String id) {
        System.out.println("执行修改小说信息的操作");
        Novel novel = novelService.getNovelList(id);
        model.addAttribute("n",novel);

        return "author/editNovel";
    }

    @RequestMapping("updateNovel.do")
    @ResponseBody
    public boolean updateNovel(Novel novel) {

        System.out.println("更新小说信息的操作");
        return novelService.updateNovel(novel);

    }

    @RequestMapping("detail.do")
    public String detail(Model model, String id) {
        System.out.println("作者进入到小说详情页");
        Novel novel = novelService.detail(id);
        model.addAttribute("n",novel);

        return "author/detail";
    }

    @RequestMapping("detailR.do")
    public String detailR(Model model, String id) {
        System.out.println("读者进入到小说详情页");
        Novel novel = novelService.detail(id);
        model.addAttribute("n",novel);

        return "workbench/fantasy/detail";
    }

    @RequestMapping("detailChapter.do")
    public String detailChapter(Model model, String id) {
        System.out.println("作者进入到小说章节详情页");
        Novel novel = novelService.detail(id);
        model.addAttribute("n",novel);

        return "setting/chapterManage";
    }

    @RequestMapping("getNovelName.do")
    @ResponseBody
    public List<String> getNovelName(String name) {
        System.out.println("取得小说名列表(进行模糊查询)");
        return novelService.getNovelName(name);
    }

}
